import QtQuick 2.0
import Ubuntu.Components 1.1
import Myface 1.0
import QtMultimedia 5.4
/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "myface.defool"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    // Removes the old toolbar and enables new features of the new header.
    useDeprecatedToolbar: false

    property var resolution
    width: units.gu(100)
    height: units.gu(75)
    function getPriateDirectory() {
        var sharepath = "/home/phablet/.local/share/";
        var path = sharepath + applicationName;
        console.log("path: " + path);
        return path;
    }
    PageStack{
        id: pageStack
        anchors.fill: parent
        Component.onCompleted: {
            console.log('pagestack created')
            pageStack.push(cameraPage)
        }
        Page {
            id: cameraPage

            Column {
                spacing: units.gu(1)
                anchors {
                    margins: units.gu(2)
                    fill: parent
                }

                CameraSelector {
                    id: selector
                    cameraObject: camera
                }

                Camera {
                    id: camera
                    deviceId: QtMultimedia.availableCameras[1].deviceId
                    position: Camera.FrontFace
                    imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash

                    exposure {
                        exposureCompensation: -1.0
                        exposureMode: Camera.ExposurePortrait
                    }

                    flash.mode: Camera.FlashRedEyeReduction

                    imageCapture {
                        onImageCaptured: {
                            console.log("image captured! reqId: " + requestId)
                            image.source = preview  // Show the preview in an Image
                            cameraPage.state = "image"
                            pageStack.pop()
                            pageStack.push(diagPage, requestId)
                        }

                        onImageSaved: {
                            console.log("image has been saved: " + requestId)
                            console.log("saved path: " + path)
//                            image.source = path;
                        }
                    }

                    Component.onCompleted: {
                        resolution = camera.viewfinder.resolution;
                        selector. selectedCameraDevice = 1;
                        camera.start();
                        console.log("resolution: " + resolution.width + " " + resolution.height);
                    }

                }
                Row {
                    id: container
                    VideoOutput {
                        id: video
                        source: camera
                        width: cameraPage.width
                        height: cameraPage.height
                        focus : visible // to receive focus and capture key events when visible
                        orientation: -90
                    }
                }

                states: [
                    State {
                        name: "image"
                        PropertyChanges {
                            target: container
                            x:-cameraPage.width
                        }
                        PropertyChanges {
                            target: capture
                            opacity:0
                        }
                    }
                ]

                transitions: [
                    Transition {
                        NumberAnimation { target: container; property: "x"; duration: 500
                            easing.type:Easing.OutSine}
                        //                NumberAnimation { target: inputcontainer; property: "opacity"; duration: 200}
                        NumberAnimation { target: capture; property: "opacity"; duration: 200}
                    }
                ]

                Button {
                    id: capture
                    anchors.bottom: parent.bottom
                    anchors.bottomMargin: units.gu(1)
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: "Capture"

                    onClicked: {
                        console.log("capture path: " + getPriateDirectory());
                        camera.imageCapture.captureToLocation(getPriateDirectory());
                        cameraPage.state = "image"
                    }
                }

            }
        }// end of page1

        Page{
            id: diagPage
            Item {
                id: view
                width: parent.width
                height: parent.height

                Image {
                    //                    anchors.fill: parent
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    width: parent.height
                    height: parent.width
                    id: image
                    rotation: 90
                    fillMode: Image.PreserveAspectFit
                }

                Text {
                    text: image.source
                    color:"red"
                    font.pixelSize: units.gu(2.5)
                    width: parent.width
                    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                }
            }
        } // end of page2

    } // end pagestatck
}

